package com.yilin.tms.system.controller;

import com.yilin.commons.exception.BusinessException;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.system.param.*;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.system.service.IParamService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/param/")
public class ParamController extends BaseController {
    @Resource
    private IParamService paramService;

    /**
     * 获取地区
     */
    @PostMapping("getAreaPage")
    public ReturnData<Object> getAreaPage(Area area) {
        List<Area> map = this.paramService.getAreaPage(area);
        return jsonView(SUCCESS_CODE, "获取列表成功", map);
    }

    /**
     * 获取银行
     */
    @PostMapping("getBankPage")
    public ReturnData<Object> getBankPage(Bank bank, Integer page, Integer limit) {
        PageData<Bank> map = this.paramService.getBankPage(bank, page, limit);
        return jsonView(SUCCESS_CODE, "获取列表成功", map);
    }

    /**
     * 获取银行
     */
    @PostMapping("getBankByCode")
    public ReturnData<Bank> getBankByCode(String code) {
        Bank bank = this.paramService.getBankByCode(code);
        return jsonView(SUCCESS_CODE, "获取成功", bank);
    }

    /**
     * 获取银行
     */
    @PostMapping("getBankList")
    public ReturnData<List<Bank>> getBankList(String bankName) {
        List<Bank> bankList = this.paramService.getBankList(bankName);
        return jsonView(SUCCESS_CODE, "获取成功", bankList);
    }

    /**
     * 获取城市 分頁
     */
    @PostMapping("getCityPage")
    public ReturnData<Object> getCityPage(City city, Integer page, Integer limit) {
        PageData<City> map = this.paramService.getCityPage(city, page, limit);
        return jsonView(SUCCESS_CODE, "获取列表成功", map);
    }

    /**
     * 获取城市 列表
     */
    @PostMapping("getCityList")
    public ReturnData<Object> getCityList(String cityName) {
        List<City> cityList = this.paramService.getCityList(cityName);
        return jsonView(SUCCESS_CODE, "获取列表成功", cityList);
    }

    /**
     * 货物类型
     */
    @PostMapping("getTreeGoodsTypeList")
    public ReturnData<List<GoodsType>> getTreeGoodsTypeList(GoodsType goodsType) {
        List<GoodsType> list = this.paramService.getTreeGoodsTypeList(goodsType);
        return jsonView(SUCCESS_CODE, "获取列表成功", list);
    }

    /**
     * 根据id获取货物类型
     */
    @PostMapping("getGoodsTypeById")
    public ReturnData<GoodsType> getGoodsTypeById(String goodsTypeId) {
        GoodsType treeGoodsTypeById = this.paramService.getGoodsTypeById(goodsTypeId);
        return jsonView(SUCCESS_CODE, "获取列表成功", treeGoodsTypeById);
    }

    /**
     * 根据code获取货物类型
     */
    @PostMapping("getGoodsTypeByCode")
    public ReturnData<List<GoodsType>> getGoodsTypeByCode(String goodsTypeCode) {
        List<GoodsType> goodsTypeList = this.paramService.getGoodsTypeByCode(goodsTypeCode);
        return jsonView(SUCCESS_CODE, "获取成功", goodsTypeList);
    }

    /**
     * 获取运输类型
     */
    @PostMapping("getTransportTypeList")
    public ReturnData<List<TransportType>> getTransportTypeList(TransportType transportType) {
        List<TransportType> list = this.paramService.getTransportTypeList(transportType);
        return jsonView(SUCCESS_CODE, "获取列表成功", list);
    }

    /**
     * 获取能源类型
     */
    @PostMapping("getTruckEnergyTypeList")
    public ReturnData<List<TruckEnergyType>> getTruckEnergyTypeList(TruckEnergyType truckEnergyType) {
        List<TruckEnergyType> map = this.paramService.getTruckEnergyTypeList(truckEnergyType);
        return jsonView(SUCCESS_CODE, "获取列表成功", map);
    }

    /**
     * 根据能源ID获取
     */
    @PostMapping("getTruckEnergyById")
    @Deprecated
    @SkipLoginVerify
    public ReturnData<TruckEnergyType> getTruckEnergyById(String truckEnergyTypeId) {
        TruckEnergyType truckEnergyType = this.paramService.getTruckEnergyById(truckEnergyTypeId);
        return jsonView(SUCCESS_CODE, "获取列表成功", truckEnergyType);
    }

    /**
     * 根据车辆ID获取车辆类型
     */
    @PostMapping("getTruckTypeById")
    @Deprecated
    @SkipLoginVerify
    public ReturnData<TruckType> getTruckTypeById(String truckTypeId) {
        TruckType truckType = this.paramService.getTruckTypeById(truckTypeId);
        return jsonView(SUCCESS_CODE, "获取列表成功", truckType);
    }

    /**
     * 根据名字获取能源类型
     */
    @PostMapping("getTruckEnergyTypeByName")
    public ReturnData<TruckEnergyType> getTruckEnergyTypeByName(String name) {
        TruckEnergyType truckEnergyTypeByName = this.paramService.getTruckEnergyTypeByName(name);
        return jsonView(SUCCESS_CODE, "获取成功", truckEnergyTypeByName);
    }

    /**
     * 获取车牌类型
     */
    @PostMapping("getTruckPlateList")
    public ReturnData<List<TruckPlate>> getTruckPlateList(TruckPlate truckPlate) {
        List<TruckPlate> map = this.paramService.getTruckPlateList(truckPlate);
        return jsonView(SUCCESS_CODE, "获取列表成功", map);
    }

    /**
     * 根据id获取车牌类型
     */
    @PostMapping("getTruckPlateById")
    public ReturnData<TruckPlate> getTruckPlateById(String truckPlateId) {
        TruckPlate truckPlateById = this.paramService.getTruckPlateById(truckPlateId);
        return jsonView(SUCCESS_CODE, "获取列表成功", truckPlateById);
    }

    /**
     * 获取车辆类型
     */
    @PostMapping("getTreeTruckTypeList")
    public ReturnData<List<TruckType>> getTreeTruckTypeList(TruckType truckType) {
        List<TruckType> list = this.paramService.getTreeTruckTypeList(truckType);
        return jsonView(SUCCESS_CODE, "获取列表成功", list);
    }

    /**
     * 获取公共车辆类型
     */
    @PostMapping("getTreeCommTruckTypeList")
    public ReturnData<List<CommTruckType>> getTreeCommTruckTypeList(CommTruckType commTruckType) {
        List<CommTruckType> list = this.paramService.getCommTruckTypeList(commTruckType);
        return jsonView(SUCCESS_CODE, "获取列表成功", list);
    }

    /**
     * 获取计价车辆类型
     */
    @PostMapping("getPricingCommTruckTypeList")
    public ReturnData<List<CommTruckType>> getPricingCommTruckTypeList() {
        List<CommTruckType> list = this.paramService.getPricingCommTruckTypeList();
        return jsonView(SUCCESS_CODE, "获取列表成功", list);
    }

    /**
     * 根据ID获取公共车辆类型
     */
    @PostMapping("getCommTruckTypeById")
    public ReturnData<CommTruckType> getCommTruckTypeById(String commTruckTypeId) {
        CommTruckType commTruckType = paramService.getCommTruckTypeById(commTruckTypeId);
        return jsonView(SUCCESS_CODE, "获取成功", commTruckType);
    }

    /**
     * 根据ID数组获取公共车辆类型
     */
    @PostMapping("getCommTruckTypeListByIds")
    public ReturnData<List<CommTruckType>> getCommTruckTypeListByIds(String[] commTruckTypeIds) {
        List<CommTruckType> commTruckTypeList = paramService.getCommTruckTypeListByIds(commTruckTypeIds);
        return jsonView(SUCCESS_CODE, "获取成功", commTruckTypeList);
    }


    /**
     * 添加/编辑地区
     */
    @PostMapping("editArea")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editArea(Area area) {
        if (area.getParentId().equals(area.getId())) throw new BusinessException("不能选择当前为父节点");
        if (StringUtil.isEmpty(area.getName())) {
            return jsonView(false, "请输入名称");
        }
        paramService.editArea(area);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑银行
     */
    @PostMapping("editBank")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editBank(Bank bank) {
        if (StringUtil.isEmpty(bank.getCnName())) {
            return jsonView(false, "请输入银行名称");
        }
        paramService.editBank(bank);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑城市
     */
    @PostMapping("editCity")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editCity(City city) {
        if (StringUtil.isEmpty(city.getCityName())) {
            return jsonView(false, "请输入城市名称");
        }
        paramService.editCity(city);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑货物类型
     */
    @PostMapping("editGoodsType")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editGoodsType(GoodsType goodsType) {
        if (goodsType.getParentId().equals(goodsType.getId())) throw new BusinessException("不能选择当前为父节点");
        if (StringUtil.isEmpty(goodsType.getName())) return jsonView(false, "请输入货物类型名称");
        paramService.editGoodsType(goodsType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑运输类型
     */
    @PostMapping("editTransportType")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editTransportType(TransportType transportType) {
        if (StringUtil.isEmpty(transportType.getName())) {
            return jsonView(false, "请输入运输类型名称");
        }
        paramService.editTransportType(transportType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑能源类型
     */
    @PostMapping("editTruckEnergyType")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editTruckEnergyType(TruckEnergyType truckEnergyType) {
        if (StringUtil.isEmpty(truckEnergyType.getName())) {
            return jsonView(false, "请输入能源类型名称");
        }
        paramService.editTruckEnergyType(truckEnergyType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑车牌
     */
    @PostMapping("editTruckPlate")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editTruckPlate(TruckPlate truckPlate) {
        if (StringUtil.isEmpty(truckPlate.getName())) return jsonView(false, "请输入车牌名称");
        paramService.editTruckPlate(truckPlate);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑车辆类型
     */
    @PostMapping("editTruckType")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editTruckType(TruckType truckType) {
        truckType.setIsTrailer(TruckType.IsTrailer.no);
        if (truckType.getParentId().equals(truckType.getId())) throw new BusinessException("不能选择当前为父节点");
        if (StringUtil.isEmpty(truckType.getName())) return jsonView(false, "请输入车辆类型名称");
        if (truckType.getTowingType() == TruckType.TowingType.trailer) truckType.setIsTrailer(TruckType.IsTrailer.yes);
        paramService.editTruckType(truckType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑公共车辆类型
     */
    @PostMapping("editCommTruckType")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editCommTruckType(CommTruckType truckType) {
        if (StringUtil.isEmpty(truckType.getName())) return jsonView(false, "请输入车辆类型名称");
        if (truckType.getParentId().equals(truckType.getId())) throw new BusinessException("不能选择当前为父节点");
        truckType.setIsTrailer(TruckType.IsTrailer.no);
        if (truckType.getTowingType() == TruckType.TowingType.trailer) truckType.setIsTrailer(TruckType.IsTrailer.yes);
        paramService.editCommTruckType(truckType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除地区
     */
    @PostMapping("deleteArea")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteArea(Area area) {
        if (area.getId().equals("0")) throw new BusinessException("根目录不能删除");
        paramService.deleteArea(area);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除银行
     */
    @RequestMapping("deleteBank")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteBank(Bank bank) {
        paramService.deleteBank(bank);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除城市
     */
    @PostMapping("deleteCity")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteCity(City city) {
        paramService.deleteCity(city);
        return jsonView(SUCCESS_CODE, "操作成功");
    }


    /**
     * (真实)删除货物类型
     */
    @PostMapping("deleteGoodsType")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteGoodsType(GoodsType goodsType) {
        if (goodsType.getId().equals("0")) throw new BusinessException("根目录不能删除");
        paramService.deleteGoodsType(goodsType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除运输类型
     */
    @PostMapping("deleteTransportType")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteTransportType(TransportType transportType) {
        paramService.deleteTransportType(transportType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除能源类型
     */
    @PostMapping("deleteTruckEnergyType")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteTruckEnergyType(TruckEnergyType truckEnergyType) {
        paramService.deleteTruckEnergyType(truckEnergyType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除车牌
     */
    @PostMapping("deleteTruckPlate")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteTruckPlate(TruckPlate truckPlate) {
        paramService.deleteTruckPlate(truckPlate);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除车辆类型
     */
    @PostMapping("deleteTruckType")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteTruckType(TruckType truckType) {
        if (truckType.getId().equals("0")) throw new BusinessException("根目录不能删除");
        paramService.deleteTruckType(truckType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除公共车辆类型
     */
    @PostMapping("deleteCommTruckType")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteCommTruckType(CommTruckType truckType) {
        if (truckType.getId().equals("0")) throw new BusinessException("根目录不能删除");
        paramService.deleteCommTruckType(truckType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 标题：根据排序级别获取车辆类型
     * 添加人：yhp 修改时间：2022年6月21日15:53:47内容：根据排序级别获取车辆类型
     * 修改人：     修改时间       修改内容：
     */
    @PostMapping("getTruckTypeListByLevel")
    public ReturnData<List<CommTruckType>> getTruckTypeListByLevel(Integer leven) {
        List<CommTruckType> pageData = paramService.getTruckTypeListByLevel(leven);
        return jsonView(SUCCESS_CODE, "获取列表成功", pageData);
    }

    /**
     * 标题：根据父类ID获取列表
     * 添加人：yhp 修改时间 ：2022年6月21日16:12:10内容：根据父类ID获取列表
     * 修改人：     修改时间       修改内容：
     */
    @RequestMapping("getTruckTypeListByParent")
    public ReturnData<List<CommTruckType>> getTruckTypeListByParent(String parentTruckType) {
        List<CommTruckType> pageData = paramService.getTruckTypeListByParent(parentTruckType);
        return jsonView(SUCCESS_CODE, "获取列表成功", pageData);
    }

    /**
     * 通过车辆名称检索车辆类型
     */
    @RequestMapping("queryTruckTypeByName")
    public ReturnData<List<TruckType>> queryTruckTypeByName(String name) {
        List<TruckType> list = paramService.queryTruckTypeByName(name);
        return jsonView(true, "获取成功", list);
    }

    /**
     * 通过能源名称检索能源类型
     */
    @PostMapping("queryTruckEnergyTypeByName")
    public ReturnData<TruckEnergyType> queryTruckEnergyTypeByName(String name) {
        TruckEnergyType truckEnergyType = paramService.queryTruckEnergyTypeByName(name);
        return jsonView(true, "获取成功", truckEnergyType);
    }

    /**
     * 通过车牌名称获取车牌类型
     */
    @PostMapping("queryTruckPlateByName")
    public ReturnData<TruckPlate> queryTruckPlateByName(String name) {
        TruckPlate truckPlate = paramService.queryTruckPlateByName(name);
        return jsonView(true, "获取成功", truckPlate);
    }

    /**
     * 获取 根据银行卡号获取银行名称
     * 仅支持个人卡
     */
    @PostMapping(value = "getBankInfoByNo")
    public ReturnData<Bank> getBankInfoByNo(String bankNo) {
        ParamUtil.isNull(bankNo, "请输入银行卡号！");
        Bank bank = this.paramService.getBankcardInfo(bankNo);
        return jsonView(true, "获取银行卡列表成功！", bank);
    }

    /**
     * 编码找查
     */
    @PostMapping("getTransportTypeByCode")
    public ReturnData<TransportType> getTransportTypeByCode(String code) {
        TransportType transportType = paramService.getTransportTypeByCode(code);
        return jsonView(true, "获取成功", transportType);
    }

    /**
     * 银行联行号查询
     */
    @PostMapping("queryBankCodePage")
    public ReturnData<PageData<BankCode>> queryBankCodePage(String bankShortName, String cityName, String bankName, Integer page, Integer limit) {
        PageData<BankCode> pageData = paramService.queryBankCodePage(bankShortName,cityName,bankName,page,limit);
        return jsonView(true, "获取成功", pageData);
    }


}
